package com.captainbank.tcl.tclapplication.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    /**
     * 获取当前时间戳
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    /**
     * 根据时间戳判断是否过期
     */
    public static boolean isOverDtae(long endTime) {
        long cSingTimeStamp = System.currentTimeMillis();
        if (cSingTimeStamp > endTime) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 时间戳转换为格式化时间，
     *
     * @param timeStamp 目标时间戳
     * @param formatStr 时间格式表达式（默认格式为yyyy-MM-dd HH:mm:ss）
     * @return
     */
    public static String timeStamp2Date(long timeStamp, String formatStr) {
        if (formatStr == null || formatStr.trim().equals("")) {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr,Locale.CHINA);
        String dateStr = simpleDateFormat.format(timeStamp);
        return dateStr;
    }

    /**
     * 格式化时间转换为时间戳，
     *
     * @param dateStr   目标格式化时间
     * @param formatStr 时间格式表达式（默认格式为yyyy-MM-dd HH:mm:ss）
     * @return
     */
    public static long date2TimeStamp(String dateStr, String formatStr) {
        if (formatStr == null || formatStr.trim().equals("")) {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr,Locale.CHINA);
        long timeStamp = 0;
        try {
            timeStamp = simpleDateFormat.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    /**
     * 得到现在时间并进行格式化，默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @param formatStr 时间格式表达式
     * @return
     */
    public static String getNowDateStr(String formatStr) {
        if (formatStr == null || formatStr.trim().equals("")) {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        }
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr,Locale.CHINA);
        String dateStr = simpleDateFormat.format(calendar.getTime());
        return dateStr;
    }

    /**
     * 根据时间戳显示个性化提示
     *
     * @param mTime 要个性化显示的时间戳(毫秒级)
     * @param type  1换行
     * @param formatStr  时间格式表达式
     * @return
     */
    public static String showTimeStr(long mTime, int type, String formatStr) {
        long curTime = System.currentTimeMillis();
        long time = curTime / (long) 1000 - mTime / (long) 1000;
        //时分
        SimpleDateFormat df = new SimpleDateFormat("HH:mm",Locale.CHINA);
        //月日时分
        SimpleDateFormat sdf;
        if (formatStr == null || formatStr.trim().equals("")) {
            if (type == 1) {
                sdf = new SimpleDateFormat("MM月dd日\nHH:mm",Locale.CHINA);
            } else {
                sdf = new SimpleDateFormat("MM月dd日 HH:mm",Locale.CHINA);
            }
        }else{
            sdf = new SimpleDateFormat(formatStr,Locale.CHINA);
        }
        String personalizedTime;
        if (time < 60 && time >= 0) {
            personalizedTime = "刚刚";
        } else if (time >= 60 && time < 3600) {
            personalizedTime = time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 12) {
            personalizedTime = time / 3600 + "小时前";
        } else if(time >= 3600 * 12 && time < 3600 * 24){
            //天数
            SimpleDateFormat dayDF = new SimpleDateFormat("dd",Locale.CHINA);
            //当前时间的天（日）数
            String curDay = dayDF.format(new Date(curTime));
            //目标时间的天（日）数
            String mDay = dayDF.format(new Date(mTime));
            if (curDay.equals(mDay)) {
                if (type == 1) {
                    personalizedTime = "今天\n" + df.format(new Date(mTime));
                } else {
                    personalizedTime = "今天" + df.format(new Date(mTime));
                }
            } else {
                if (type == 1) {
                    personalizedTime = "昨天\n" + df.format(new Date(mTime));
                } else {
                    personalizedTime = "昨天" + df.format(new Date(mTime));
                }
            }
        }else {
            personalizedTime = sdf.format(new Date(mTime));
        }
        return personalizedTime;
    }

    /**
     * 根据时间戳显示个性化时间间隔
     *
     * @param type        类型（1:不显示秒 ）
     * @param periodTimes 时间差(毫秒级)
     * @return
     */
    public static String showPeriodStr(int type, long periodTimes) {
        long periodSeconds = periodTimes / 1000L;
        String result;
        if (periodSeconds > 0) {
            if (periodSeconds > 3600 * 24) {
                result = periodSeconds / (3600 * 24) + "天";
            } else if (periodSeconds < 3600 * 24 && periodSeconds > 3600) {
                result = periodSeconds / 3600 + "小时";
            } else if (periodSeconds < 3600 && periodSeconds > 60) {
                result = periodSeconds / 60 + "分钟";
            } else {
                if (type == 1) {
                    result = "";
                } else {
                    result = periodSeconds + "秒";
                }
            }
        } else {
            result = "";
        }
        return result;
    }

}
